from django.shortcuts import render, HttpResponse, redirect
from django.utils.safestring import mark_safe
from utils.pagination import Page

def tpl(request):
	return render(request, 'tpl.html')


from utils import pagination

LIST = []
for i in range(209):
	LIST.append(i)


def user_list(request):
	current_page = request.GET.get('p',1)
	current_page = int(current_page)

	page_obj = Page(current_page, len(LIST))
	data = LIST[page_obj.start:page_obj.end]

	page_str = page_obj.page_str("/user_list/")
	return render(request, 'user_list.html', {'li':data,'page_str':page_str})

############################## cookie ####################################
user_info = {
	'abc':{'pwd':'123'},
	'efg':{'pwd':'456'},
}

def login(request):
	if request.method == "GET":
		return render(request,'login.html')
	if request.method == "POST":
		u = request.POST.get('username')
		p = request.POST.get('password')
		dic = user_info.get(u)
		if not dic:
			return render(request,'login.html')
		if dic['pwd'] == p:
			res = redirect('/index/')
			res.set_cookie('username111',u, max_age=10)
			return res
		else:
			return render(request,'login.html')

def index(request):
	v = request.COOKIES.get('username111')
	if not v:
		return redirect('/login/')
	return render(request, 'index.html',{'current_user':v})

def cookie(request):
	request.COOKIES
	request.COOKIES.get('username111')

	response = render(request,'index.html')
	response = redirect('/index/')
	response.set_cookie('key','value')